<?php

/* Author: tbloc@cantho.gov.vn */
/* Created Date : 27-07-2014 15:08:58 */

class M_Dat_hang {

//Properties
    private $DH_ID;
    private $DH_NGAYDAT;
    private $DH_NGAYNHAN;
    private $DH_SOLUONG;
    private $HH_ID;
    private $DH_HOTEN;
    private $DH_MSSV;
    private $DH_LOP;
    private $DH_NGAYSINH;
    private $DH_DIACHI;
    private $DH_SDT;
    private $DH_EMAIL;
    private $DH_GHICHU;
    private $TK_ID;
    private $HH_TEN;
    private $HH_DONGIA;
    private $DH_TRANGTHAI;
    private $TK_USERNAME;
    private $DH_TENCTY;
    private $DH_DIACHICTY;
    private $DH_SDTCTY;

//properties set get 
    public function setDH_TENCTY($DH_TENCTY) {
        $this->DH_TENCTY = $DH_TENCTY;
    }

    public function getDH_TENCTY() {
        return $this->DH_TENCTY;
    }

    public function setDH_DIACHICTY($DH_DIACHICTY) {
        $this->DH_DIACHICTY = $DH_DIACHICTY;
    }

    public function getDH_DIACHICTY() {
        return $this->DH_DIACHICTY;
    }

    public function setDH_SDTCTY($DH_SDTCTY) {
        $this->DH_SDTCTY = $DH_SDTCTY;
    }

    public function getDH_SDTCTY() {
        return $this->DH_SDTCTY;
    }

    public function setDH_ID($DH_ID) {
        $this->DH_ID = $DH_ID;
    }

    public function getHH_TEN() {
        return $this->HH_TEN;
    }

    public function setTK_USERNAME($TK_USERNAME) {
        $this->TK_USERNAME = $TK_USERNAME;
    }

    public function getTK_USERNAME() {
        return $this->TK_USERNAME;
    }

    public function setHH_TEN($HH_TEN) {
        $this->HH_TEN = $HH_TEN;
    }

    public function getDH_ID() {
        return $this->DH_ID;
    }

    public function setHH_DONGIA($HH_DONGIA) {
        $this->HH_DONGIA = $HH_DONGIA;
    }

    public function getHH_DONGIA() {
        return $this->HH_DONGIA;
    }

    public function setDH_TRANGTHAI($DH_TRANGTHAI) {
        $this->DH_TRANGTHAI = $DH_TRANGTHAI;
    }

    public function getDH_TRANGTHAI() {
        return $this->DH_TRANGTHAI;
    }

    public function setTK_ID($TK_ID) {
        $this->TK_ID = $TK_ID;
    }

    public function getTK_ID() {
        return $this->TK_ID;
    }

    public function setDH_NGAYDAT($DH_NGAYDAT) {
        $this->DH_NGAYDAT = $DH_NGAYDAT;
    }

    public function getDH_NGAYDAT() {
        return $this->DH_NGAYDAT;
    }

    public function setDH_NGAYNHAN($DH_NGAYNHAN) {
        $this->DH_NGAYNHAN = $DH_NGAYNHAN;
    }

    public function getDH_NGAYNHAN() {
        return $this->DH_NGAYNHAN;
    }

    public function setDH_SOLUONG($DH_SOLUONG) {
        $this->DH_SOLUONG = $DH_SOLUONG;
    }

    public function getDH_SOLUONG() {
        return $this->DH_SOLUONG;
    }

    public function setHH_ID($HH_ID) {
        $this->HH_ID = $HH_ID;
    }

    public function getHH_ID() {
        return $this->HH_ID;
    }

    public function setDH_HOTEN($DH_HOTEN) {
        $this->DH_HOTEN = $DH_HOTEN;
    }

    public function getDH_HOTEN() {
        return $this->DH_HOTEN;
    }

    public function setDH_MSSV($DH_MSSV) {
        $this->DH_MSSV = $DH_MSSV;
    }

    public function getDH_MSSV() {
        return $this->DH_MSSV;
    }

    public function setDH_LOP($DH_LOP) {
        $this->DH_LOP = $DH_LOP;
    }

    public function getDH_LOP() {
        return $this->DH_LOP;
    }

    public function setDH_NGAYSINH($DH_NGAYSINH) {
        $this->DH_NGAYSINH = $DH_NGAYSINH;
    }

    public function getDH_NGAYSINH() {
        return $this->DH_NGAYSINH;
    }

    public function setDH_DIACHI($DH_DIACHI) {
        $this->DH_DIACHI = $DH_DIACHI;
    }

    public function getDH_DIACHI() {
        return $this->DH_DIACHI;
    }

    public function setDH_SDT($DH_SDT) {
        $this->DH_SDT = $DH_SDT;
    }

    public function getDH_SDT() {
        return $this->DH_SDT;
    }

    public function setDH_EMAIL($DH_EMAIL) {
        $this->DH_EMAIL = $DH_EMAIL;
    }

    public function getDH_EMAIL() {
        return $this->DH_EMAIL;
    }

    public function setDH_GHICHU($DH_GHICHU) {
        $this->DH_GHICHU = $DH_GHICHU;
    }

    public function getDH_GHICHU() {
        return $this->DH_GHICHU;
    }

//crud methods
    public function insert($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO dat_hang(                        			
                    DH_NGAYDAT, 
                    DH_NGAYNHAN, 
                    DH_SOLUONG, 
                    HH_ID, 
                    DH_HOTEN, 
                    DH_MSSV, 
                    DH_LOP, 
                    DH_NGAYSINH, 
                    DH_DIACHI, 
                    DH_SDT, 
                    DH_EMAIL, 
                    DH_GHICHU, 
                    TK_ID, 
                    DH_TRANGTHAI,
                    DH_TENCTY,
                    DH_DIACHICTY,
                    DH_SDTCTY
		)
	VALUES(    		
                    NOW(), 
                    :DH_NGAYNHAN, 
                    :DH_SOLUONG, 
                    :HH_ID, 
                    :DH_HOTEN, 
                    :DH_MSSV, 
                    :DH_LOP, 
                    :DH_NGAYSINH, 
                    :DH_DIACHI, 
                    :DH_SDT, 
                    :DH_EMAIL, 
                    :DH_GHICHU, 
                    :TK_ID, 
                    :DH_TRANGTHAI,
                    :DH_TENCTY,
                    :DH_DIACHICTY,
                    :DH_SDTCTY
                        
	    );";
            $parameters = array( 
                ":DH_NGAYNHAN",
                ":DH_SOLUONG",
                ":HH_ID",
                ":DH_HOTEN",
                ":DH_MSSV",
                ":DH_LOP",
                ":DH_NGAYSINH",
                ":DH_DIACHI",
                ":DH_SDT",
                ":DH_EMAIL",
                ":DH_GHICHU",
                ":TK_ID",
                ":DH_TRANGTHAI",                
                ":DH_TENCTY",
                ":DH_DIACHICTY",
                ":DH_SDTCTY"
            );
            $values = array(  
                $this->getDH_NGAYNHAN(),
                $this->getDH_SOLUONG(),
                $this->getHH_ID(),
                $this->getDH_HOTEN(),
                $this->getDH_MSSV(),
                $this->getDH_LOP(),
                $this->getDH_NGAYSINH(),
                $this->getDH_DIACHI(),
                $this->getDH_SDT(),
                $this->getDH_EMAIL(),
                $this->getDH_GHICHU(),
                $this->getTK_ID(),
                $this->getDH_TRANGTHAI(),
                $this->getDH_TENCTY(),
                $this->getDH_DIACHICTY(),
                $this->getDH_SDTCTY()
            );
            $types = array(                                
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_INT,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR
            );
            $lastInsertID = $db->executeNonQuery($sql, $parameters, $values, $types, true);
            $this->setDH_ID($lastInsertID);
        }
    }

    public function delete($db = null) {
        if ($db != null) {
            $sql = "DELETE FROM dat_hang where DH_ID= :DH_ID;";
            $parameters = array(":DH_ID");
            $values = array($this->getDH_ID());
            $types = array(PDO::PARAM_INT); //Kiem tra lai kieu du lieu nhe
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }

    public function update($db = null) {
        if ($db != null) {
            $sql = "UPDATE dat_hang SET                                         
                    DH_NGAYNHAN=:DH_NGAYNHAN,
                    DH_SOLUONG=:DH_SOLUONG,
                    HH_ID=:HH_ID,
                    DH_HOTEN=:DH_HOTEN,
                    DH_MSSV=:DH_MSSV,
                    DH_LOP=:DH_LOP,
                    DH_NGAYSINH=:DH_NGAYSINH,
                    DH_DIACHI=:DH_DIACHI,
                    DH_SDT=:DH_SDT,
                    DH_EMAIL=:DH_EMAIL,
                    DH_GHICHU=:DH_GHICHU,
                    DH_TENCTY=:DH_TENCTY,
                    DH_DIACHICTY=:DH_DIACHICTY,
                    DH_SDTCTY=:DH_SDTCTY
                    WHERE DH_ID = :DH_ID;";
            $parameters = array(
                ":DH_ID",
                ":DH_NGAYDAT",
                ":DH_NGAYNHAN",
                ":DH_SOLUONG",
                ":HH_ID",
                ":DH_HOTEN",
                ":DH_MSSV",
                ":DH_LOP",
                ":DH_NGAYSINH",
                ":DH_DIACHI",
                ":DH_SDT",
                ":DH_EMAIL",
                ":DH_GHICHU",
                ":DH_TENCTY",
                ":DH_DIACHICTY",
                ":DH_SDTCTY"
            );
            $values = array(
                $this->getDH_ID(),
                $this->getDH_NGAYDAT(),
                $this->getDH_NGAYNHAN(),
                $this->getDH_SOLUONG(),
                $this->getHH_ID(),
                $this->getDH_HOTEN(),
                $this->getDH_MSSV(),
                $this->getDH_LOP(),
                $this->getDH_NGAYSINH(),
                $this->getDH_DIACHI(),
                $this->getDH_SDT(),
                $this->getDH_EMAIL(),
                $this->getDH_GHICHU(),
                $this->getDH_TENCTY(),
                $this->getDH_DIACHICTY(),
                $this->getDH_SDTCTY()
            );
            $types = array(
                PDO::PARAM_INT,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_INT,
                PDO::PARAM_INT,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR
            );
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }

    public function getByID($db = null) {
        if ($db != null) {
            $sql = "SELECT
                    DH_ID,
                    DH_NGAYDAT,
                    DH_NGAYNHAN,
                    DH_SOLUONG,
                    HH_ID,
                    DH_HOTEN,
                    DH_MSSV,
                    DH_LOP,
                    DH_NGAYSINH,
                    DH_DIACHI,
                    DH_SDT,
                    DH_EMAIL,
                    DH_GHICHU,
                    TK_ID,
                    DH_TRANGTHAI,
                    DH_TENCTY,
                    DH_DIACHICTY,
                    DH_SDTCTY
                    FROM dat_hang WHERE DH_ID = :DH_ID";
            $parameters = array(":DH_ID");
            $values = array($this->getDH_ID());
            $types = array(PDO::PARAM_INT); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setDH_ID($row->DH_ID);
                $this->setDH_NGAYDAT($row->DH_NGAYDAT);
                $this->setDH_NGAYNHAN($row->DH_NGAYNHAN);
                $this->setDH_SOLUONG($row->DH_SOLUONG);
                $this->setHH_ID($row->HH_ID);
                $this->setDH_HOTEN($row->DH_HOTEN);
                $this->setDH_MSSV($row->DH_MSSV);
                $this->setDH_LOP($row->DH_LOP);
                $this->setDH_NGAYSINH($row->DH_NGAYSINH);
                $this->setDH_DIACHI($row->DH_DIACHI);
                $this->setDH_SDT($row->DH_SDT);
                $this->setDH_EMAIL($row->DH_EMAIL);
                $this->setDH_GHICHU($row->DH_GHICHU);
                $this->setTK_ID($row->TK_ID);
                $this->setDH_TRANGTHAI($row->DH_TRANGTHAI);
                $this->setDH_TENCTY($row->DH_TENCTY);
                $this->setDH_DIACHICTY($row->DH_DIACHICTY);
                $this->setDH_SDTCTY($row->DH_SDTCTY);
            }
            return $this;
        }
    }

    public function getList($db = null) {
        if ($db != null) {
            $sql = "SELECT
                    DH_ID,
                    
                    DATE_FORMAT(DH_NGAYDAT, '%d/%m/%Y') as DH_NGAYDAT,
                    DATE_FORMAT(DH_NGAYNHAN, '%d/%m/%Y') as DH_NGAYNHAN,
                    DH_SOLUONG,
                    hh.HH_ID,
                    DH_HOTEN,
                    DH_MSSV,
                    DH_LOP,
                    DATE_FORMAT(DH_NGAYSINH, '%d/%m/%Y') as DH_NGAYSINH,
                    DH_DIACHI,
                    DH_SDT,
                    DH_EMAIL,
                    DH_GHICHU,
                    HH_TEN,
                    HH_DONGIA,
                    DH_TRANGTHAI,
                    DH_TENCTY,
                    DH_DIACHICTY,
                    DH_SDTCTY,
                    tk.TK_USERNAME,
                    tk.TK_ID,
                    (HH_DONGIA*DH_SOLUONG) as THANH_TIEN
                    FROM
                    dat_hang dh LEFT JOIN hang_hoa hh
                    ON dh.HH_ID=hh.HH_ID JOIN tai_khoan tk
                    ON dh.TK_ID=tk.TK_ID
                    ORDER BY DH_ID DESC";
            $rows = $db->executeQuery($sql);
            $objects = array();
            if ($rows != null) {
                while ($row = $rows->fetch()) {
                    $obj = new M_Dat_hang();
                    $obj->setDH_ID($row->DH_ID);
                    $obj->setDH_NGAYDAT($row->DH_NGAYDAT);
                    $obj->setDH_NGAYNHAN($row->DH_NGAYNHAN);
                    $obj->setDH_SOLUONG($row->DH_SOLUONG);
                    $obj->setHH_ID($row->HH_ID);
                    $obj->setDH_HOTEN($row->DH_HOTEN);
                    $obj->setDH_MSSV($row->DH_MSSV);
                    $obj->setDH_LOP($row->DH_LOP);
                    $obj->setDH_NGAYSINH($row->DH_NGAYSINH);
                    $obj->setDH_DIACHI($row->DH_DIACHI);
                    $obj->setDH_SDT($row->DH_SDT);
                    $obj->setDH_EMAIL($row->DH_EMAIL);
                    $obj->setDH_GHICHU($row->DH_GHICHU);
                    $obj->setTK_ID($row->TK_ID);
                    $obj->setHH_DONGIA($row->HH_DONGIA);
                    $obj->setHH_TEN($row->HH_TEN);
                    $obj->setTK_USERNAME($row->TK_USERNAME);
                    $obj->setDH_TRANGTHAI($row->DH_TRANGTHAI);
                    $obj->setDH_TENCTY($row->DH_TENCTY);
                    $obj->setDH_DIACHICTY($row->DH_DIACHICTY);
                    $obj->setDH_SDTCTY($row->DH_SDTCTY);
                    $objects[] = $obj;
                }
                return $objects;
            }
        }
    }

    public function capnhatTrangthai($db = null) {
        if ($db != null) {
            $sql = "UPDATE dat_hang set DH_TRANGTHAI=:DH_TRANGTHAI WHERE DH_ID = :DH_ID;";
            $parameters = array(":DH_TRANGTHAI", ":DH_ID");
            $values = array($this->getDH_TRANGTHAI(), $this->getDH_ID());
            $types = array(PDO::PARAM_STR, PDO::PARAM_INT);
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }

    public function thongke($db = null, $trangthai) {
        if ($db != null) {
            $sql = "SELECT COUNT(DH_TRANGTHAI) FROM dat_hang WHERE DH_TRANGTHAI=:trangthai";
            $parameters = array(":trangthai");
            $values = array($trangthai);
            $types = array(PDO::PARAM_STR);
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }

}
?>

